package com.byxy.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.byxy.aspect.MyLog;
import com.byxy.entity.Agency;
import com.byxy.entity.Guide;
import com.byxy.service.GuideService;

@RequestMapping(value = "/guide")
@Controller
public class GuideContrller {
	@Resource
	private GuideService guideService;

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add(Model model) {
		model.addAttribute("agencys", guideService.findAgency());
		return "guide/add";
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(Guide guide, Integer agencyId) {
		Agency agency = new Agency();
		if (agencyId == null)
			agency = null;
		else
			agency.setId(agencyId);
		guide.setAgency(agency);
		guideService.save(guide);
		return "redirect:/guide/list";
	}

	@MyLog(desc = "删除")
	@RequestMapping(value = "delete", method = RequestMethod.GET)
	public String delete(int id) {
		guideService.delete(id);
		return "redirect:/guide/list";
	}

	@MyLog(desc = "显示列表")
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model) {
		model.addAttribute("list", guideService.listAll());
		return "guide/list";
	}

	@MyLog(desc = "数据更新")
	@RequestMapping(value = "update", method = RequestMethod.GET)
	public String update(int id, Model model) {
		model.addAttribute("guide", guideService.getById(id));
		model.addAttribute("agencys", guideService.findAgency());
		return "guide/update";
	}

	@MyLog(desc = "数据更新")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(Guide guide, Integer agencyId) {
		Agency agency = new Agency();
		if (agencyId == null)
			agency = null;
		else
			agency.setId(agencyId);
		guide.setAgency(agency);
		guideService.update(guide);
		return "redirect:/guide/list";
	}
}
